package demo.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class CallerTask implements Callable<String> {
    public String call() throws Exception {
        return "Hello from " + Thread.currentThread().getName();
    }

    public static void main(String[] args) {
        // 创建异步任务
        CallerTask task = new CallerTask();
        FutureTask<String> future = new FutureTask<String>(task);
        // 启动线程
        Thread t1 = new Thread(future, "新线程");
        t1.start();
        try {
            // 等待执行完成，并获取返回结果
            String result = future.get();
            System.out.println(result);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}
